package org.linlinjava.litemall.db.service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.beetl.sql.core.query.LambdaQuery;
import org.linlinjava.litemall.db.domain.LitemallSystem;
import org.linlinjava.litemall.db.domain.LitemallSystemExample;
import org.linlinjava.litemall.db.mapper.LitemallSystemDao;
import org.springframework.stereotype.Service;

@Service
public class LitemallSystemConfigService {
    @Resource
    private LitemallSystemDao systemMapper;

    public Map<String, String> queryAll() {
    	
    	
    	LitemallSystem query = new LitemallSystem();
    	query.setDeleted(false);
    	 List<LitemallSystem> systemList = systemMapper.template(query);
			/*
			 * LitemallSystemExample example = new LitemallSystemExample();
			 * example.or().andDeletedEqualTo(false);
			 * 
			 * List<LitemallSystem> systemList = systemMapper.selectByExample(example);
			 */
        Map<String, String> systemConfigs = new HashMap<>();
        for (LitemallSystem item : systemList) {
            systemConfigs.put(item.getKeyName(), item.getKeyValue());
        }

        return systemConfigs;
    }

    public Map<String, String> listMail() {
    	
    	LambdaQuery<LitemallSystem>  query = systemMapper.createLambdaQuery();
    	
    	query.andLike(LitemallSystem::getKeyName, "litemall_mall_%");
    	query.andEq(LitemallSystem::getDeleted, false);
    	List<LitemallSystem> systemList =  query.select();
		/*
		 * LitemallSystemExample example = new LitemallSystemExample();
		 * example.or().andKeyNameLike("litemall_mall_%").andDeletedEqualTo(false);
		 * List<LitemallSystem> systemList = systemMapper.selectByExample(example);
		 */
        Map<String, String> data = new HashMap<>();
        for(LitemallSystem system : systemList){
            data.put(system.getKeyName(), system.getKeyValue());
        }
        return data;
    }

    public Map<String, String> listWx() {
    	
    	LambdaQuery<LitemallSystem>  query = systemMapper.createLambdaQuery();
    	
    	query.andLike(LitemallSystem::getKeyName, "litemall_wx_%");
    	query.andEq(LitemallSystem::getDeleted, false);
    	List<LitemallSystem> systemList =  query.select();
    	
		/*
		 * LitemallSystemExample example = new LitemallSystemExample();
		 * example.or().andKeyNameLike("litemall_wx_%").andDeletedEqualTo(false);
		 * List<LitemallSystem> systemList = systemMapper.selectByExample(example);
		 */
        Map<String, String> data = new HashMap<>();
        for(LitemallSystem system : systemList){
            data.put(system.getKeyName(), system.getKeyValue());
        }
        return data;
    }

    public Map<String, String> listOrder() {
    	

    	LambdaQuery<LitemallSystem>  query = systemMapper.createLambdaQuery();
    	
    	query.andLike(LitemallSystem::getKeyName, "litemall_order_%");
    	query.andEq(LitemallSystem::getDeleted, false);
    	List<LitemallSystem> systemList =  query.select();
    	
		/*
		 * LitemallSystemExample example = new LitemallSystemExample();
		 * example.or().andKeyNameLike("litemall_order_%").andDeletedEqualTo(false);
		 * List<LitemallSystem> systemList = systemMapper.selectByExample(example);
		 */
        Map<String, String> data = new HashMap<>();
        for(LitemallSystem system : systemList){
            data.put(system.getKeyName(), system.getKeyValue());
        }
        return data;
    }

    public Map<String, String> listExpress() {
    	
    	LambdaQuery<LitemallSystem>  query = systemMapper.createLambdaQuery();
    	
    	query.andLike(LitemallSystem::getKeyName, "litemall_express_%");
    	query.andEq(LitemallSystem::getDeleted, false);
    	List<LitemallSystem> systemList =  query.select();
    	
    	
		/*
		 * LitemallSystemExample example = new LitemallSystemExample();
		 * example.or().andKeyNameLike("litemall_express_%").andDeletedEqualTo(false);
		 * List<LitemallSystem> systemList = systemMapper.selectByExample(example);
		 */
        Map<String, String> data = new HashMap<>();
        for(LitemallSystem system : systemList){
            data.put(system.getKeyName(), system.getKeyValue());
        }
        return data;
    }

    public void updateConfig(Map<String, String> data) {
    	
        for (Map.Entry<String, String> entry : data.entrySet()) {
        	LitemallSystem udpate = new LitemallSystem();
        	udpate.setKeyValue(entry.getValue());
        	udpate.setUpdateTime(LocalDateTime.now());
        	
        	LambdaQuery<LitemallSystem>  query = systemMapper.createLambdaQuery();
        	query.andEq(LitemallSystem::getKeyName, entry.getKey());
        	query.andEq(LitemallSystem::getDeleted, false);
        	query.updateSelective(udpate);
        	
			/*
			 * LitemallSystemExample example = new LitemallSystemExample();
			 * example.or().andKeyNameEqualTo(entry.getKey()).andDeletedEqualTo(false);
			 * 
			 * LitemallSystem system = new LitemallSystem();
			 * system.setKeyName(entry.getKey()); system.setKeyValue(entry.getValue());
			 * system.setUpdateTime(LocalDateTime.now());
			 * systemMapper.updateByExampleSelective(system, example);
			 */
        }

    }

    public void addConfig(String key, String value) {
        LitemallSystem system = new LitemallSystem();
        system.setKeyName(key);
        system.setKeyValue(value);
        system.setAddTime(LocalDateTime.now());
        system.setUpdateTime(LocalDateTime.now());
        systemMapper.insertTemplate(system,true);
        //systemMapper.insertSelective(system);
    }
}
